<template>
    <jb-crud-page
        ref="pageRef"
        searchUrl="/aiKnowledgeBase/list"
        :conditions-align="'start'"
        :searchConditions="pageConditions"
        :editComponent="KnowledgeEdit"
    >
        <template #conditions-form>
            <jb-input
                v-model:value="pageConditions.keywords"
                placeholder="请输入关键词搜索"
                style="width: 200px"
            />
            <n-select
                v-model:value="pageConditions.enable"
                :options="[{ label: '启用', value: true}, { label: '禁用', value: false}]"
                placeholder="是否启用"
                :clearable="true"
                style="width: 160px"
            />
        </template>

        <template #opt>
            <jb-btn
                type="primary"
                secondary
                @click="pageRef.showEditModal('新增AI知识库',{appId:props.appId})"
            >新增AI知识库</jb-btn>
        </template>

        <template #default="{list,tableStartIndex}">
            <jb-table
                :data="list"
                :start-index="tableStartIndex"
            >
                <jb-column type="seq" title="序号" width="60" fixed="left" />
                <jb-column
                    field="name"
                    title="名称"
                    min-width="200"
                    fixed="left"
                />
                <jb-column
                    field="briefInfo"
                    title="简介"
                    min-width="200"
                />
                <jb-column
                    field="AI_FILE"
                    title="AI_FILE"
                    min-width="120"
                >
                  <template #default="{ row }">
                    <jb-btn
                        type="primary"
                        icon="weui:qr-code-outlined"
                        ghost
                        :drawerComponent="File"
                        :drawerComponentProps="{id:row.id}"
                        :drawerArgs="{
                          width: '1400px'
                        }"
                    >文件训练</jb-btn>
                  </template>
                </jb-column>
                <jb-column
                    field="AI_DOC"
                    title="AI_DOC"
                    min-width="120"
                >
                  <template #default="{ row }">
                    <jb-btn
                        type="primary"
                        icon="ri:file-check-line"
                        ghost
                        :drawerComponent="Doc"
                        :drawerComponentProps="{id:row.id}"
                        :drawerArgs="{
                          width: '1400px'
                        }"
                    >结果</jb-btn>
                  </template>
                </jb-column>
                <jb-column
                    field="dataType"
                    title="是否启用"
                    min-width="100"
                >
                  <template #default="{ row }">
                    <jb-switch v-model:value="row.enable" :url="`/aiKnowledgeBase/toggleEnable/${row.id}`" @update:value="pageRef.loadData()"></jb-switch>
                  </template>
                </jb-column>

                <jb-column
                    title="操作"
                    width="120"
                    fixed="right"
                >
                    <template #default="{ row }">
                        <jb-btn
                            class="mx-8px"
                            :icon="Icons.EDIT"
                            secondary
                            circle
                            type="warning"
                            @click="pageRef.showEditModal('编辑AI知识库',{id:row.id})"
                        />
                        <jb-btn
                            type="error"
                            class="mx-8px"
                            :icon="Icons.DELETE"
                            secondary
                            circle
                            :confirmText="'确认删除该知识库？'"
                            :url="`/aiKnowledgeBase/del/${row.id}`"
                            @success="pageRef.loadData()"
                        />
                    </template>
                </jb-column>
            </jb-table>
        </template>
    </jb-crud-page>
</template>

<script setup>
import { ref } from 'vue'
import { Icons } from '@/constants'
import { useMessage } from 'naive-ui'
import { useResetableData } from '@/hooks/common/use-resetable-data'
import KnowledgeEdit from './components/knowledge_edit.vue'
import File from '@/views/ai_file/index.vue'
import Doc from '@/views/ai_doc/index.vue'

const message = useMessage()
const pageRef = ref(null)
const props = defineProps({
    appId: String
})
console.log('知识库页面props', props)

// 页面查询条件
const pageConditions = useResetableData({
    keywords: '',
    databaseId: null,
    appId: props.appId
})

</script>

<style scoped>
.jb-crud-page :deep(.conditions-form) {
    gap: 12px;
}
</style>
